#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 10
#define S 30
#define C 6



typedef struct student
{
    long num;
    char name[M];
    float score[C];
    float sum;
    float ave;
}STU;



int Menu(void);
void Read(STU stu[],int n,int m);
void SumandAve(STU stu[],int n,int m);


int D(float a,float b);
void ByScore(STU stu[],int n,int m,int(*compare)(float a,float b));
void SF(float *x,float *y);
void SL(long *x,long *y);
void SC(char x[],char y[]);
void Print(STU stu[],int n,int m);
void ByName(STU stu[],int n,int m);


void SearchByNumber(STU stu[],int n,int m);
void SrarchByName(STU stu[],int n,int m);
void Write(STU record[],int n,int m);
void Reader(STU record[],int *n,int *m);



int main()
{
    char c;
    int n=0;
    int m=0;
    STU stu[S];
    printf("Input student number(n<%d):",S);
    scanf("%d",&n);
    printf("Input course number(n<%d):",C);
    scanf("%d",&m);
    do
    {
        c=Menu();
        switch(c)
        {
            case 1:Read(stu,n,m);
                    break;
            case 2:SumandAve(stu,n,m);
                    break;
            case 3:ByScore(stu,n,m,D);
                    Print(stu,n,m);
                    break;
            case 4:ByName(stu,n,m);
                    Print(stu,n,m);
                    break;
            case 5:SearchByNumber(stu,n,m);
                    break;
            case 6:SrarchByName(stu,n,m);
                    break;
            case 7:Write(stu,n,m);
                    break;
            case 8:Reader(stu,&n,&m);
                    break;
            case 0:printf("End\n");
                    exit(0);
        }
    }
    while(1);
}




int Menu(void)
{
    printf("1.Append record\n");
    printf("2.Caculate total and average score of every student\n");
    printf("3.Sort in ascending order by total score of every student\n");
    printf("4.Sort in dictionary order by name\n");
    printf("5.Search by number\n");
    printf("6.Search by name\n");
    printf("7.Write to a file\n");
    printf("8.Read from a file\n");
    printf("0.Exit\n");
    int it;
    printf("Please choose:");
    scanf("%d",&it);
    return it;
}


void Read(STU stu[],int n,int m)
{
    int i,j;
    printf("id,name,score:\n");
    for(i=0;i<n;i++)
    {
        scanf("%ld%s",&stu[i].num,stu[i].name);
        for(j=0;j<m;j++)
            {
              scanf("%f",&stu[i].score[j]);
            }
    }
}


void SumandAve(STU stu[],int n,int m)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        stu[i].sum=0;
        for(j=0;j<m;j++)
        {
            stu[i].sum=stu[i].sum+stu[i].score[j];
        }
        stu[i].ave=stu[i].sum/m;
        printf("student %d:sum=%.0f,ave=%.0f\n",i+1,stu[i].sum,stu[i].ave);
    }
}



int D(float a,float b)
{
    return a>b;
}


void ByScore(STU stu[],int n,int m,int(*compare)(float a,float b))
{
    int i,j,k,l;
    for(i=0;i<n-1;i++)
    {
        k=i;
        for(j=i+1;j<n;j++)
        {
            if((*compare)(stu[j].sum,stu[k].sum))k=j;
        }
        if(k!=i)
        {
            for(l=0;l<m;l++)
            {
                SF(&stu[k].score[l],&stu[i].score[l]);
            }
            SF(&stu[k].sum,&stu[i].sum);
            SF(&stu[k].ave,&stu[i].ave);
            SL(&stu[k].num,&stu[i].num);
            SC(stu[k].name,stu[i].name);
        }
    }
}


void SF(float *x,float *y)
{
   float t;
   t=*x;
   *x=*y;
   *y=t;
}


void SL(long *x,long *y)
{
    long t;
    t=*x;
    *x=*y;
    *y=t;
}


void SC(char x[],char y[])
{
    char t[M];
    strcpy(t,x);
    strcpy(x,y);
    strcpy(y,t);
}


void Print(STU stu[],int n,int m)
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("%ld\t%s\t",stu[i].num,stu[i].name);
        for(j=0;j<m;j++)
        {
            printf("%.0f\t",stu[i].score[j]);
        }
        printf("%.0f\t%.0f\n",stu[i].sum,stu[i].ave);
    }
}


void ByName(STU stu[],int n,int m)
{
    int i,j,t;
    for(i=0;i<n-1;i++)
    {
        if(strcmp(stu[i].name,stu[i].name)<0)
        {
            for(t=0;t<m;t++)
            {
                SF(&stu[i].score[t],&stu[j].score[t]);
            }
            SF(&stu[i].sum,&stu[j].sum);
            SF(&stu[i].ave,&stu[j].ave);
            SL(&stu[i].num,&stu[j].num);
            SC(stu[i].name,stu[j].name);
        }
    }
}


void SearchByNumber(STU stu[],int n,int m)
{
    long number;
    int i,j;
    printf("number:");
    scanf("%ld",&number);
    for(i=0;i<n;i++)
    {
        if(stu[i].num==number)
        {
            printf("%ld\t%s\t",stu[i].num,stu[i].name);
            for(j=0;j<m;j++)
            {
                printf("%.0f\t",stu[i].score[j]);
            }
            printf("%.0f\t%.0f\n",stu[i].sum,stu[i].ave);
            return;
        }
    }
}


void SrarchByName(STU stu[],int n,int m)
{
    char x[M];
    int i,j;
    printf("name:");
    scanf("%s",x);
    for(i=0;i<n;i++)
    {
        if(strcmp(stu[i].name,x)==0)
        {
            printf("%ld\t%s\t",stu[i].num,stu[i].name);
            for(j=0;j<m;j++)
            {
                printf("%.0f\t",stu[i].score[j]);
            }
            printf("%.0f\t%.0f\n",stu[i].sum,stu[i].ave);
            return;
        }
    }
}

void Write(STU stu[],int n,int m)
{
    FILE *fp;
    int i,j;
    if((fp=fopen("student.txt","w"))==NULL)
    {
        printf("Fail to open.\n");
        exit(0);
    }
    fprintf(fp,"%d\t%d\n",n,m);
    for(i=0;i<n;i++)
    {
        fprintf(fp,"%10ld%10s",stu[i].num,stu[i].name);
        for(j=0;j<m;j++)
        {
            fprintf(fp,"%10.0f",stu[i].score[j]);
        }
        fprintf(fp,"%10.0f%10.0f\n",stu[i].sum,stu[i].ave);
    }
    fclose(fp);
}

void Reader(STU stu[],int *n,int *m)
{
    FILE *fp;
    int i,j;
    if((fp=fopen("student.txt","r"))==NULL)
    {
        printf("Fail to open.\n");
        exit(0);
    }
    fscanf(fp,"%d\t%d",n,m);
    for(i=0;i<*n;i++)
    {
        fscanf(fp,"%10ld",&stu[i].num);
        fscanf(fp,"%10s",stu[i].name);
        for(j=0;j<*m;j++)
        {
            fscanf(fp,"%10f",&stu[i].score[j]);
        }
        fscanf(fp,"%10f%10f",&stu[i].sum,&stu[i].ave);
    }
    fclose(fp);
}
